package com.example.dao.implDao;

import com.example.dao.RoleDao;
import com.example.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoleDaoImpl implements RoleDao {
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    public RoleDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public List<Role> roleList() {
        String sql = "SELECT * FROM t_role";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Role.class));
    }

    @Override
    public List<Role> getRoleByname(String rname) {
        String sql = "SELECT * FROM t_role WHERE rname like ?";
        String pattern = "%" + rname + "%";
        return jdbcTemplate.query(sql, new Object[]{pattern}, new BeanPropertyRowMapper<>(Role.class));
    }

}
